package com.schoolMall.service.impl;

import com.schoolMall.dao.ShopDao;
import com.schoolMall.domain.Shop;
import com.schoolMall.dto.ImageHolder;
import com.schoolMall.dto.ShopDto;
import com.schoolMall.enums.ShopStateEnum;
import com.schoolMall.exception.ShopOperationException;
import com.schoolMall.service.ShopService;
import com.schoolMall.utils.ImgUtil;
import com.schoolMall.utils.PathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.Objects;

/**
 * @author:zyh
 * @Time:2021-08-29-15:14
 * @email:1269231889@qq.com
 */
@Service
public class ShopServiceImpl implements ShopService {
    private Logger logger = LoggerFactory.getLogger(ShopServiceImpl.class);
    @Autowired
    private ShopDao shopDao;

    @Override
    @Transactional
    public ShopDto addShop(Shop shop, ImageHolder imageHolder) throws ShopOperationException {
        if (Objects.isNull(shop)) {
            return new ShopDto(ShopStateEnum.NULL_SHOP);
        }
        try {
            shop.setEnableStatus(ShopStateEnum.CHECK.getState());
            shop.setCreateTime(new Date());
            shop.setLastEditTime(new Date());
            int num = shopDao.insertShop(shop);
            if (num <= 0) {
                throw new ShopOperationException("insertShop fail");
            }
            if (Objects.nonNull(imageHolder)) {
                try {
                    addShopImg(shop, imageHolder);
                } catch (Exception e) {
                    throw new ShopOperationException("addShopImg error");
                }
                num = shopDao.updateShop(shop);
                if (num <= 0) {
                    throw new ShopOperationException("更新图片失败");
                }
            }

        } catch (Exception e) {
            throw new ShopOperationException("addShop.error" + e.getMessage());
        }
        return new ShopDto(ShopStateEnum.CHECK, shop);
    }

    @Override
    public Shop queryShopById(long shopId) {
        return shopDao.queryByShopId(shopId);
    }

    @Override
    @Transactional
    public ShopDto modifyShop(Shop shop, ImageHolder imageHolder) throws ShopOperationException {
        if (Objects.isNull(shop) || shop.getShopId() == null) {
            return new ShopDto(ShopStateEnum.NULL_SHOP);
        }
        Shop tempShop = shopDao.queryByShopId(shop.getShopId());
        if (Objects.nonNull(tempShop.getShopImg()) && Objects.nonNull(imageHolder) && !StringUtils.pathEquals("", imageHolder.getImageName())) {
            try {
                logger.info("deleteFile start:{}", tempShop.getShopImg());
                ImgUtil.deleteFileOrPath(tempShop.getShopImg());
                addShopImg(shop, imageHolder);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //更新店铺信息
        shop.setLastEditTime(new Date());
        try {
            int num = shopDao.updateShop(shop);
            if (num <= 0) {
                return new ShopDto(ShopStateEnum.INNER_ERROR);
            } else {
                Shop shopNew = shopDao.queryByShopId(shop.getShopId());
                return new ShopDto(ShopStateEnum.CHECK, shopNew);
            }
        } catch (Exception e) {
            throw new ShopOperationException("modify shop exception" + e.getMessage());
        }
    }

    private void addShopImg(Shop shop, ImageHolder imageHolder) throws IOException {
        //获取shop图片目录的相对路径
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = ImgUtil.generateThumbnail(imageHolder, dest);
        shop.setShopImg(shopImgAddr);
    }
}
